package controllers;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import models.User;
import play.Logger;
import play.libs.Codec;
import play.libs.Files;
import play.mvc.Controller;

public class Utility extends Controller {

	// 获取用户
	public static User getUser() {
		String email = session.get("email");
		User user = User.find("byEmail", email).first();
		return user;
	}

	public static void changePass(String pass1, String pass2) {

		validation.required(pass1).message("密码不能为空");
		validation.required(pass2).message("密码不能为空");
		// 密码6-12位
		User user = getUser();
		if (!validation.hasErrors()) {
			if (pass1.equals(pass2)) {
				user.password = Codec.hexMD5(pass1);
				user.uuid = null;
				// 这里必须手动进行保存
				user.save();
				Logger.info("设置的密码为：" + pass1);
				renderText("设置成功");
			} else {
				renderText("两次密码输入不同");
			}
		} else {
			renderText(validation.errors().get(0));
		}
	}

	// 生成随机问题
	public static void genQuestion() {

	}

	// 文本内容的过滤，禁止js执行等
	public static void contentFilter() {

	}

	// 防止越权操作
	public static boolean checkPermiss(Long id) {
		String email = session.get("email");
		User user = User.findById(id);
		if (user != null) {
			if (user.email.equals(email)) {
				return true;
			}
		}
		return false;
	}
	
}